﻿package src.hardware 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import src.hardware.rgbledmatrixdisplay.LED;
	import src.interfaces.IMeggyJrRGB;
	import src.interfaces.IRGBLEDMatrixDisplay;
	import src.hardware.rgbledmatrixdisplay.RGBLEDMatrixDisplay;
	import src.interfaces.ITopLEDDisplay;
	
	/**
	* ...
	* @author Louis Tovar
	*/
	public class MeggyJrRGB extends Sprite implements IMeggyJrRGB
	{
		private var _mainDisplay:IRGBLEDMatrixDisplay;
		private var _topDisplay:ITopLEDDisplay;
		private var _audioTransducer:LoFiAudioTransducer;
		private var _userInput:UserInput;
		
		private var bufferRedrawCount:int;
		private var totalBuffers:int;
		
		public function MeggyJrRGB() 
		{
			initSelf()
		}
		
		private function initSelf():void
		{
			bufferRedrawCount = 0;
			totalBuffers = 20;
			
			_mainDisplay = new RGBLEDMatrixDisplay(8, 8);
			addChild(mainDisplay as DisplayObject);
			
			mainDisplay.x = 135;
			mainDisplay.y = 50;
			mainDisplay.width = 195;
			mainDisplay.height = 195;
			
			_topDisplay = new TopLEDDisplay(8, mainDisplay.width);
			addChild(topDisplay as DisplayObject);
			
			topDisplay.x = mainDisplay.x;
			topDisplay.y = 33;
			
			//addEventListener(Event.ENTER_FRAME, affectMeggyScreen)
		}
		
		private function affectMeggyScreen(e:Event):void 
		{
			var r:uint = (Math.random() * 0xFF - LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL) + LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL;
			var g:uint = (Math.random() * 0xFF - LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL) + LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL;
			var b:uint = (Math.random() * 0xFF - LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL) + LED.EMULATOR_LOWEST_COLOR_PER_CHANNEL;
			
			var randomColor:uint = r << 16 | g << 8 | b;
			
			mainDisplay.DrawPx(int(Math.random() * 8), int(Math.random() * 8), randomColor);
			
			bufferRedrawCount++;
			
			if (bufferRedrawCount == totalBuffers)
			{
				mainDisplay.ShowScreen()
				bufferRedrawCount = 0;
				
				
				
				if ( !int(Math.random() * 5 - 1) )
				{
					mainDisplay.EmptyScreen();
				}
				
				topDisplay.SetAuxLEDs(uint(Math.random()*255));
			}
		}
		
		public function get mainDisplay():IRGBLEDMatrixDisplay { return _mainDisplay; }
		
		public function get topDisplay():ITopLEDDisplay { return _topDisplay; }
		
		public function get audioTransducer():LoFiAudioTransducer { return _audioTransducer; }
		
		public function get userInput():UserInput { return _userInput; }
		
	}
	
}